var page = require('webpage').create();
page.viewportSize = { width: 1366, height: 768 };

page.onResourceError = function(resourceError) {
  console.log('Error Load', resourceError.url, resourceError.errorString);
};

page.onResourceRequested = function(requestData, networkRequest) {
  if (/data:/.test(requestData.url)) { //base64图片请求url太长
    return
  }
  console.log('Request', requestData.url);
  console.log('Request params', JSON.stringify(requestData.headers))
  networkRequest.setHeader('Host', '10.1.62.234')
  // networkRequest.setHeader('Referer', 'http://10.1.62.234/show/')
  networkRequest.setHeader('cookie', 'language=zh_CN; token=cf22cb7ea31ca56fbff96579fc70e959e938514a594809a51209a6a5d4589003; skin=blue')

  // console.log(networkRequest)
};

page.onResourceReceived = function(response) {
  if ((/data:/.test(response.url))) {
    return
  }
  console.log('Response', response.url, response.status);
};

page.onPrompt = function(msg, defaultVal) {
  if (msg === "What's your name?") {
    return 'PhantomJS';
  }
  return defaultVal;
};

page.onError = function(msg, trace) {
  console.log('Error', msg)
}

page.onNavigationRequested = function(url, type, willNavigate, main) {
  console.log('Trying to navigate to', url);
  // console.log('Caused by: ' + type);
  // console.log('Will actually navigate: ' + willNavigate);
  // console.log('Sent from the page\'s main frame: ' + main);
}
page.onUrlChanged = function(targetUrl) {
  console.log('New URL', targetUrl);
};

page.open('http://10.1.62.234/show/', {
  operation: "GET",
  encoding: "utf8",
  headers: {
    'Host': '10.1.62.234',
    'Referer': 'http://10.1.62.234/show/',
    'Cookie': 'language=zh_CN; token=cf22cb7ea31ca56fbff96579fc70e959e938514a594809a51209a6a5d4589003; skin=blue'
  }
}, function (s) {
  setInterval(function() {
    // page.render('tmp/show.png');
    // phantom.exit();
  }, 10000);
});